package com.baidu.location;

import android.app.Service;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.location.Location;
import android.net.wifi.ScanResult;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Messenger;
import android.os.Process;
import android.util.Log;
import java.io.File;
import java.io.RandomAccessFile;
import java.util.List;

public final class f extends Service
{
  static String a = Environment.getExternalStorageDirectory().getPath() + "/baidu/tempdata";
  private static String e = a + "/glb.dat";
  private static File f = null;
  private static File g = null;
  private long A = 0L;
  private boolean B = true;
  private c C = null;
  private SQLiteDatabase D = null;
  private String E = "bdcltb09";
  private String F = null;
  private String G = null;
  private boolean H = false;
  private boolean I = false;
  private String J = null;
  private int K = 0;
  private boolean L = true;
  private double M = 0.0D;
  private double N = 0.0D;
  private double O = 0.0D;
  private long P = 0L;
  private boolean Q = false;
  final Handler b = new ad(???);
  final Messenger c = new Messenger(???.b);
  private String d = a + "/vm.dat";
  private t h = null;
  private n i = null;
  private w j = null;
  private l k = null;
  private z l = null;
  private z m = null;
  private z n = null;
  private u o = null;
  private u p = null;
  private u q = null;
  private Location r = null;
  private String s = null;
  private String t = null;
  private String u = null;
  private boolean v = false;
  private boolean w = true;
  private boolean x = false;
  private boolean y = false;
  private long z = 0L;

  private String a(String paramString)
  {
    Object localObject2;
    String str1 = null;
    ak.e();
    if ((((this.o == null) || (!(this.o.a())))) && (this.h != null))
      this.o = this.h.d();
    this.F = this.o.c();
    if (this.o != null)
    {
      localObject1 = this.o;
      localObject2 = new StringBuffer(64);
      ((StringBuffer)localObject2).append(String.format("cell=%d|%d|%d|%d:%d", new Object[] { Integer.valueOf(((u)localObject1).c), Integer.valueOf(((u)localObject1).d), Integer.valueOf(((u)localObject1).a), Integer.valueOf(((u)localObject1).b), Integer.valueOf(((u)localObject1).f) }));
      ak.a("baidu_location_service", ((StringBuffer)localObject2).toString());
      if ((((this.l == null) || (!(this.l.g())))) && (this.j != null))
        this.l = this.j.h();
      if (this.l == null)
        break label376;
      ak.a("baidu_location_service", this.l.d());
      if ((this.i == null) || (!(this.i.e())))
        break label387;
      this.r = this.i.d();
      if (this.k != null)
        str1 = this.k.b();
      if (3 != ae.b(this))
        break label395;
    }
    label387: label395: for (Object localObject1 = "&cn=32"; ; localObject1 = String.format("&cn=%d", new Object[] { Integer.valueOf(this.h.c()) }))
      while (true)
      {
        while (true)
        {
          while (true)
          {
            localObject2 = localObject1;
            if (this.w)
            {
              String str2 = an.c();
              localObject2 = localObject1;
              if (str2 != null)
                localObject2 = ((String)localObject1) + str2;
            }
            str1 = ((String)localObject2) + str1;
            localObject1 = str1;
            if (paramString != null)
              localObject1 = paramString + str1;
            return ak.a(this.o, this.l, this.r, (String)localObject1, 0);
            ak.a("baidu_location_service", "cellInfo null...");
          }
          label376: ak.a("baidu_location_service", "wifi list null");
        }
        this.r = null;
      }
  }

  private String a(boolean paramBoolean)
  {
    if ((((this.o == null) || (!(this.o.a())))) && (this.h != null))
      this.o = this.h.d();
    String str = this.o.c();
    if ((this.D == null) || (str == null))
    {
      ak.e();
      this.H = false;
    }
    while (true)
    {
      if (!(this.H))
        break label458;
      if (!(paramBoolean))
        break;
      return String.format("{\"result\":{\"time\":\"" + ak.a() + "\",\"error\":\"66\"},\"content\":{\"point\":{\"x\":\"%f\",\"y\":\"%f\"},\"radius\":\"%f\",\"isCellChanged\":\"%b\"}}", new Object[] { Double.valueOf(this.M), Double.valueOf(this.N), Double.valueOf(this.O), Boolean.valueOf(true) });
      ak.e();
      if ((System.currentTimeMillis() - this.P < 1500L) || (str.equals(this.G)))
        continue;
      this.H = false;
      try
      {
        Cursor localCursor;
        do
        {
          localCursor = this.D.rawQuery("select * from " + this.E + " where id = \"" + str + "\";", null);
          this.G = str;
          this.P = System.currentTimeMillis();
        }
        while (localCursor == null);
        if (localCursor.moveToFirst())
        {
          new StringBuilder("lookup DB success:").append(this.G);
          ak.e();
          this.M = (localCursor.getDouble(1) - 1235.4322999999999D);
          this.O = (localCursor.getDouble(2) - 4326.0D);
          this.N = (localCursor.getDouble(3) - 2367.3217D);
          this.H = true;
          new StringBuilder("lookup DB success:x").append(this.M).append("y").append(this.N).append("r").append(this.O);
          ak.e();
        }
        localCursor.close();
      }
      catch (Exception localException)
      {
        this.P = System.currentTimeMillis();
      }
    }
    return String.format("{\"result\":{\"time\":\"" + ak.a() + "\",\"error\":\"68\"},\"content\":{\"point\":{\"x\":\"%f\",\"y\":\"%f\"},\"radius\":\"%f\",\"isCellChanged\":\"%b\"}}", new Object[] { Double.valueOf(this.M), Double.valueOf(this.N), Double.valueOf(this.O), Boolean.valueOf(this.L) });
    if (paramBoolean)
      label458: return "{\"result\":{\"time\":\"" + ak.a() + "\",\"error\":\"67\"}}";
    return "{\"result\":{\"time\":\"" + ak.a() + "\",\"error\":\"63\"}}";
  }

  public static void a()
  {
    try
    {
      if (e != null)
      {
        Object localObject = new File(e);
        g = (File)localObject;
        if (((File)localObject).exists())
          return;
        localObject = new File(a);
        if (!(((File)localObject).exists()))
          ((File)localObject).mkdirs();
        g.createNewFile();
        localObject = new RandomAccessFile(g, "rw");
        ((RandomAccessFile)localObject).seek(0L);
        ((RandomAccessFile)localObject).writeInt(-1);
        ((RandomAccessFile)localObject).writeInt(-1);
        ((RandomAccessFile)localObject).writeInt(0);
        ((RandomAccessFile)localObject).writeLong(0L);
        ((RandomAccessFile)localObject).writeInt(0);
        ((RandomAccessFile)localObject).writeInt(0);
        ((RandomAccessFile)localObject).close();
        return;
      }
      g = null;
      return;
    }
    catch (Exception localException)
    {
      g = null;
    }
  }

  public static String b()
  {
    ak.e();
    return null;
  }

  private void g()
  {
    label224: float f2;
    int i3;
    int i4;
    int i5;
    Object localObject2;
    if (this.x)
      return;
    if (System.currentTimeMillis() - this.z < 1000L)
    {
      ak.e();
      if (this.t != null)
      {
        this.k.a(this.t);
        h();
        return;
      }
    }
    ak.e();
    ak.a("baidu_location_service", "start network locating ...");
    this.x = true;
    Object localObject1 = this.p;
    if (this.h != null)
    {
      this.o = this.h.d();
      if (this.o != localObject1)
        if ((this.o != null) && (localObject1 != null));
    }
    for (int i6 = 1; ; i6 = 0)
      while (true)
      {
        this.L = i6;
        localObject2 = this.m;
        if (this.j == null)
          break label496;
        this.l = this.j.h();
        if (localObject2 == this.l)
          break label496;
        if ((this.l != null) && (localObject2 != null))
          break label224;
        i1 = 1;
        if ((i1 != 0) || (this.L) || (this.t == null))
          break label501;
        this.k.a(this.t);
        h();
        return;
        if (((u)localObject1).a(this.o))
          break;
        i6 = 1;
      }
    localObject1 = this.l;
    float f1 = ak.L;
    if ((localObject1 == null) || (localObject2 == null));
    for (int i1 = 0; ; i1 = 0)
    {
      while (true)
      {
        while (true)
        {
          if (i1 != 0)
            break label496;
          i1 = 1;
        }
        localObject1 = ((z)localObject1).a;
        localObject2 = ((z)localObject2).a;
        if (localObject1 == localObject2)
          i1 = 1;
        if ((localObject1 == null) || (localObject2 == null))
          i1 = 0;
        i4 = ((List)localObject1).size();
        i5 = ((List)localObject2).size();
        f2 = i4 + i5;
        if ((i4 != 0) || (i5 != 0))
          break;
        i1 = 1;
      }
      if ((i4 != 0) && (i5 != 0))
        break;
    }
    int i2 = 0;
    i1 = 0;
    if (i2 < i4)
    {
      String str = ((ScanResult)((List)localObject1).get(i2)).BSSID;
      if (str == null)
        break label649;
      i3 = 0;
      if (i3 >= i5)
        break label649;
      if (str.equals(((ScanResult)((List)localObject2).get(i3)).BSSID))
        i1 += 1;
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              while (true)
                i2 += 1;
              i3 += 1;
            }
            String.format("same %d,total %f,rate %f...", new Object[] { Integer.valueOf(i1), Float.valueOf(f2), Float.valueOf(f1) });
            ak.e();
            if (i1 * 2 < f2 * f1)
              break;
            i1 = 1;
          }
          i1 = 0;
        }
        label496: i1 = 0;
      }
      label501: localObject2 = a(null);
      if (localObject2 == null)
      {
        localObject1 = "{\"result\":{\"time\":\"" + ak.a() + "\",\"error\":\"62\"}}";
        this.k.a((String)localObject1);
        h();
        return;
      }
      localObject1 = localObject2;
      if (this.s != null)
      {
        localObject1 = ((String)localObject2) + this.s;
        this.s = null;
      }
      if (ae.a((String)localObject1, this.b))
      {
        this.p = this.o;
        this.m = this.l;
      }
      while (true)
      {
        if (this.w == true)
          this.w = false;
        this.z = System.currentTimeMillis();
        return;
        label649: ak.e();
      }
    }
  }

  private void h()
  {
    this.x = false;
    if ((this.t == null) || (!(ae.a(this))))
      return;
    ae.a();
  }

  public final IBinder onBind(Intent paramIntent)
  {
    return this.c.getBinder();
  }

  public final void onCreate()
  {
    File localFile1;
    Thread.setDefaultUncaughtExceptionHandler(new ab(this, this));
    this.h = new t(this, this.b);
    this.j = new w(this, this.b);
    this.k = new l(this.b);
    this.h.a();
    this.j.a();
    this.v = true;
    this.x = false;
    this.y = false;
    try
    {
      File localFile2 = new File(a);
      localFile1 = new File(a + "/ls.db");
      if (!(localFile2.exists()))
        localFile2.mkdirs();
      boolean bool = localFile1.exists();
      label155: if (bool);
    }
    catch (Exception localException1)
    {
      try
      {
        localFile1.createNewFile();
        if (localFile1.exists())
        {
          this.D = SQLiteDatabase.openOrCreateDatabase(localFile1, null);
          this.D.execSQL("CREATE TABLE IF NOT EXISTS " + this.E + "(id CHAR(40) PRIMARY KEY,time DOUBLE,tag DOUBLE, type DOUBLE , ac INT);");
        }
        ak.e();
        Log.d("baidu_location_service", "baidu location service start1 ..." + Process.myPid());
        return;
      }
      catch (Exception localException2)
      {
        break label155:
        localException1 = localException1;
      }
    }
  }

  public final void onDestroy()
  {
    if (this.h != null)
      this.h.b();
    if (this.j != null)
      this.j.c();
    if (this.i != null)
      this.i.b();
    an.b();
    this.x = false;
    this.y = false;
    this.v = false;
    if (this.C != null)
      this.C.a();
    if (this.D != null)
      this.D.close();
    ak.e();
    Log.d("baidu_location_service", "baidu location service stop ...");
    Process.killProcess(Process.myPid());
  }

  public final int onStartCommand(Intent paramIntent, int paramInt1, int paramInt2)
  {
    ak.e();
    return 2;
  }
}